package com.zmn.plat.business.impl.supplier;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zmn.plat.business.interfaces.supplier.ScmSupplierBService;
import com.zmn.plat.model.entity.supplier.ScmSupplier;
import com.zmn.plat.model.entity.supplier.SupplierCompanyMap;
import com.zmn.plat.services.interfaces.supplier.ScmSupplierCompanyMapService;
import com.zmn.plat.services.interfaces.supplier.ScmSupplierService;

/**
 * @author DaiZhichao
 * @version v1.0 类说明：ScmSupplierServiceImpl
 * @since 2018/10/26 18:02
 */
@Service
public class ScmSupplierBServiceImpl implements ScmSupplierBService {

    private Logger logger = LoggerFactory.getLogger(ScmSupplierBServiceImpl.class);

    @Resource
    ScmSupplierService supplierService;
    @Resource
    ScmSupplierCompanyMapService mapService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateByKey(ScmSupplier supplier, List<Integer> companyIdList) {

        supplierService.updateByKey(supplier);
        Integer supplierId = supplier.getSupplierId();
        List<SupplierCompanyMap> supplierCompanyMapList = companyIdList.stream().map(companyId -> new SupplierCompanyMap(supplierId, companyId)).collect(Collectors.toList());
        mapService.deleteBySupplierId(supplierId);
        if (!supplierCompanyMapList.isEmpty()) {
            mapService.insertBatch(supplierCompanyMapList);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(ScmSupplier supplier, List<Integer> companyIdList) {

        supplierService.insert(supplier);
        List<SupplierCompanyMap> supplierCompanyMapList = companyIdList.stream().map(companyId -> new SupplierCompanyMap(supplier.getSupplierId(), companyId)).collect(Collectors.toList());
        if (!supplierCompanyMapList.isEmpty()) {
            mapService.insertBatch(supplierCompanyMapList);
        }
    }
}
